/******************************************************************************************************
Copyright (C) 2017 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/

/*globals BaseConverter, MassItem, File, TextStream, settings, mass, Env, mainWindow, Dir, Application, Str*/
/*jslint plusplus: true, indent: 4*/

/** Default constructor of the converter
 * Defines the file filter
*/
function MassPeakTableConverter() {
	"use strict";
	BaseConverter.call(this);
	this.exportFileFilter = "Mass Peak Table (*.csv *.txt)";
}

MassPeakTableConverter.prototype = new BaseConverter();

MassPeakTableConverter.prototype.wantsExport = function (aPageItems) {
	"use strict";

	if (!Application.mass) {
		return false;
	}

	var i, msItem, activePlot;

	for (i = 0; i < aPageItems.length; i++) {
		if (aPageItems[i].name === "Mass Spectrum") {
			msItem = new MassItem(aPageItems[i]);
			if (msItem.isValid()) {
				activePlot = msItem.plot();
				if (activePlot && activePlot.type === MassItem.DataTypes.Spectrum && activePlot.peaks && activePlot.peaks.length) {
					return true;
				}
			}
		}
	}
	return false;
};

MassPeakTableConverter.prototype.exportItems = function (aPageItems, aFilename) {
	"use strict";
	var strm, i, j, msItem, plot, plotPeaks, file, max, maxIntensity, mzValue, abundance;

	try {
		file = new File(aFilename);
		if (file.open(File.WriteOnly)) {
			strm = new TextStream(file);
			strm.precision = settings.value("MassPeakTableConverter/ExportDecimals", 6);

			for (i = 0; i < aPageItems.length; i++) {
				msItem = new MassItem(aPageItems[i]);
				if (msItem.isValid()) {
					plot = msItem.plot(); //active Plot
					if (plot && plot.type === MassItem.DataTypes.Spectrum) {
						strm.writeln(Str.linifyAndQuote(plot.description));
						plotPeaks = plot.peaks;

						maxIntensity = 1.0;
						max = 0;
						for (j = 1; j < plotPeaks.length; ++j) {
							if (plotPeaks[j].center.y > plotPeaks[max].center.y) {
								max = j;
							}
						}
						if (max) {
							maxIntensity = plotPeaks[max].center.y;
						}

						for (j = 0; j < plotPeaks.length; ++j) {
							mzValue = plotPeaks[j].center.x;
							abundance = plotPeaks[j].center.y * 100.0 / maxIntensity;
							strm.writeln(mzValue, '\t', abundance, '\t');
						}

						if (plotPeaks.length) {
							strm.writeln();
						}
					}
				}
			}
		}
	} finally {
		file.close();
	}
	return 0;
};

Env.Converters.push(new MassPeakTableConverter());

function MassPeakTableConverterTest() {
	"use strict";
	var retVal = -1,
		massPeakTableConv = new MassPeakTableConverter(),
		pageItems = mainWindow.activeDocument.selection();

	if (massPeakTableConv.wantsExport(pageItems)) {
		retVal = massPeakTableConv.exportItems(pageItems, Dir.home() + "/MassPeakTable.txt");
	}
	return retVal;
}
